package com.dbb.trade.entity.order;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Transient;

import com.dbb.trade.entity.IdEntity;
import com.dbb.trade.entity.company.Company;
import com.dbb.trade.entity.status.OrderStatus;
import com.dbb.trade.entity.status.OrderType;
import com.dbb.trade.entity.status.Payment;


@Entity
public class Orders extends IdEntity{
	
	private Integer orderNum;
	private String orderRef;
	private OrderType type = OrderType.PROCUREMENT;
	private Date orderDate = new Date();
	private Payment payment;
	private String deliveryPlace;
	private Date deliveryDate;
	private Company supplier;
	private String supplierId;
	private Company customer;
	private String customerId;
	
	private OrderStatus status = OrderStatus.PREPARE;
	
	private List<OrderItem> items;
	
	private String productionDesc;
	private String inspection;
	private String discrepancyAndClaim;
	
	public Orders() { }
	
	public Orders(Company customer, Company supplier) {
		this.customer = customer;
		this.supplier = supplier;
	}
	public String getOrderRef() {
		return orderRef;
	}
	public void setOrderRef(String orderRef) {
		this.orderRef = orderRef;
	}
	public OrderType getType() {
		return type;
	}
	public void setType(OrderType type) {
		this.type = type;
	}
	public Date getOrderDate() {
		return orderDate;
	}
	public void setOrderDate(Date orderDate) {
		this.orderDate = orderDate;
	}
	public Payment getPayment() {
		return payment;
	}
	public void setPayment(Payment payment) {
		this.payment = payment;
	}
	
	public String getDeliveryPlace() {
		return deliveryPlace;
	}
	public void setDeliveryPlace(String deliveryPlace) {
		this.deliveryPlace = deliveryPlace;
	}
	public Date getDeliveryDate() {
		return deliveryDate;
	}
	public void setDeliveryDate(Date deliveryDate) {
		this.deliveryDate = deliveryDate;
	}
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "SUPPLIER_ID", insertable = false, updatable = false)
	public Company getSupplier() {
		return supplier;
	}
	public void setSupplier(Company supplier) {
		this.supplier = supplier;
	}
	
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "CUSTOMER_ID", insertable = false, updatable = false)
	public Company getCustomer() {
		return customer;
	}
	public void setCustomer(Company customer) {
		this.customer = customer;
	}
	@OneToMany
	@JoinColumn(name = "ORDER_ID")
	public List<OrderItem> getItems() {
		return items;
	}
	public void setItems(List<OrderItem> items) {
		this.items = items;
	}
	public String getProductionDesc() {
		return productionDesc;
	}
	public void setProductionDesc(String productionDesc) {
		this.productionDesc = productionDesc;
	}
	public String getInspection() {
		return inspection;
	}
	public void setInspection(String inspection) {
		this.inspection = inspection;
	}
	public String getDiscrepancyAndClaim() {
		return discrepancyAndClaim;
	}
	public void setDiscrepancyAndClaim(String discrepancyAndClaim) {
		this.discrepancyAndClaim = discrepancyAndClaim;
	}
	public OrderStatus getStatus() {
		return status;
	}
	public void setStatus(OrderStatus status) {
		this.status = status;
	}

	@Column(nullable = false)
	public Integer getOrderNum() {
		return orderNum;
	}

	public void setOrderNum(Integer orderNum) {
		this.orderNum = orderNum;
	}
	@Transient
	public Float getTotalPrice() {
		Float price = 0f;
		if(items != null) {
			for(OrderItem item : items) {
				price += item.getTotalPrice();
			}
		}
		return price;
	}

	public void addItem(OrderItem item) {
		if(items == null) {
			items = new ArrayList<OrderItem>();
		}
		items.add(item);
	}
	@Column(name = "SUPPLIER_ID")
	public String getSupplierId() {
		return supplierId;
	}

	public void setSupplierId(String supplierId) {
		this.supplierId = supplierId;
	}
	@Column(name = "CUSTOMER_ID")
	public String getCustomerId() {
		return customerId;
	}

	public void setCustomerId(String customerId) {
		this.customerId = customerId;
	}
	
}
